<template>
  <div>
    <!-- 导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/SystemHomePage' }"
        >首页</el-breadcrumb-item
      >
      <el-breadcrumb-item>管理员管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员信息</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="40">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getCommentList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGrabble"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 用户列表区 -->
      <el-table :data="currentPageData" stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="时间" prop="date"></el-table-column>
        <el-table-column label="用户名" prop="commentUsers"></el-table-column>
        <el-table-column label="内容" prop="content"></el-table-column>
        <el-table-column label="点赞">
        <template slot-scope="scope">
        <el-rate
         v-model="nice[scope.$index + (queryInfo.pageNum - 1) * queryInfo.pageSize]"
         disabled
         show-score
         text-color="#ff9900"
        >
        </el-rate>
        </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="removeUserById(scope.row._id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pageNum: 1, // 当前页数，默认为1
        pageSize: 5, // 每页显示数量
      },
      CommentList: [], //所有数据
      currentPageData: [], //当前页显示内容
      total: 1, //页面所有条数
      FuzzyQuery: null,
      addDialogVisible:false, //控制添加信息出现于隐藏
      updateDialogVisible:false,//控制修改信息出现于隐藏
      userID: "",
      nice:[],
    };
  },
  mounted() {
    this.getCommentList();
  },
  methods: {
    //显示评论列表
    async getCommentList() {
      const { data: result } = await this.$http.get("CommentList", {
        params: this.queryInfo,
      });
      if (!result.ret) return this.$message.error("用户列表失败");
      this.CommentList = result.data;
      this.currentPageData = result.data;
      let newArr = this.currentPageData.map(item=>item.nice)
      for (let i = 0; i < newArr.length; i++) {
         this.nice.push( Number(newArr[i]))
      } 
      //显示共多少条
      this.total = this.CommentList.length;
      this.handleSizeChange();
    },
    //评论查询
    async getGrabble() {
      const getGrabbleValue = await this.$http.post("GrabbleComment", {
        params: this.queryInfo.query,
      });
      const FuzzyQuerydata = getGrabbleValue.data.data.GrabbleComment;
      if (FuzzyQuerydata != "") {
        this.currentPageData = FuzzyQuerydata;
      } else {
        this.currentPageData = this.CommentList;
      }
    },
    // 获取想要删除值的id
    async removeUserById(id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该评论, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      //如果用户删除返回字符串confirm,如果用户不删除则返回字符串cancel
      if (confirmResult !== "confirm") {
        return this.$Message.info("您已取消该操作");
      } else {
        //通过id进行查找删除
        const { data: result } = await this.$http.delete("removeComment", {
          data: { id },
        });
        if (result.errorCode !== 0) {
          return this.$Message.error("删除失败");
        } else {
          this.$Message.success("删除成功");
          this.getCommentList();
        }
      }
    },
    //监听pageSize改变是事件
    handleSizeChange(newSize) {
      //获取当前页面共多少条信息
      if (newSize) {
        this.queryInfo.pageSize = newSize;
      } else {
        this.queryInfo.pageSize;
      }
      let begin = (this.queryInfo.pageNum - 1) * this.queryInfo.pageSize;
      let end = this.queryInfo.pageNum * this.queryInfo.pageSize;
      this.currentPageData = this.CommentList.slice(begin, end);
    },
    //监听页码值改变是事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.handleSizeChange(this.pageSize);
    },
  },
};
</script>
<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
</style>